﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Board.Models
{
    public class Site
    {
        private bool _exist = false;
        private HashSet<Unit> _units = null;
        private HashSet<Unit> _accessibleUnits = null; 

        public static string ALL_SITES = "ALL_SITES";

        public string SiteId { get; set; }

        public bool Exist
        {
            get
            {
                if (!_exist && !string.IsNullOrEmpty(SiteId))
                {
                    using (DbEntities db = new DbEntities())
                    {
                        _exist = (from u in db.t_AllUnitBed
                                  where u.SiteID.Equals(SiteId)
                                  select u).Any();
                    }
                }
                return _exist;
            }
        }

        public HashSet<Unit> Units
        {
            get
            {
                if (_units == null)
                {
                    using (DbEntities db = new DbEntities())
                    {
                        var beds =(from u in db.t_AllUnitBed
                                 where u.SiteID.Equals(SiteId)
                                 select new {u.SiteID, u.UnitID, u.Bed}).Select(
                                     u => new Bed() {SiteId = u.SiteID, UnitId = u.UnitID, BedId = u.Bed});
                        _units = new HashSet<Unit>(beds.Select(b=>new{b.SiteId, b.UnitId}).Distinct().Select(u=>new Unit(){SiteId = u.SiteId, UnitId = u.UnitId}));
                        foreach (var unit in _units)
                        {
                            unit.Beds = new HashSet<Bed>(beds.Where(b=>b.UnitId.Equals(unit.UnitId)));
                        }
                    }
                }
                return _units;
            }
            set { _units = value; }
        } 

        public HashSet<Unit> AccessibleUnit
        {
            get
            {
                return _accessibleUnits;
            }
            set { _accessibleUnits = value; }
        } 

        public HashSet<Bed> Beds
        {
            get
            {
                if (Units.Any())
                {
                    HashSet<Bed> beds = new HashSet<Bed>();
                    foreach (var unit in _units)
                    {
                        beds.UnionWith(unit.Beds);
                    }
                    return beds;
                }
                return new HashSet<Bed>();
            }
        } 

        public Site(string siteId)
        {
            using (DbEntities db = new DbEntities())
            {
                if ((from s in db.t_AllUnitBed
                     where s.SiteID.Equals(siteId)
                     select s).Any())
                {
                    SiteId = siteId;
                    _exist = true;
                }
                else
                {
                    _exist = false;
                }
            }
        }


        public Site()
        {
        }

        public bool Equals(Site site)
        {
            if (!string.IsNullOrEmpty(SiteId) && SiteId.Equals(site.SiteId))
            {
                return Exist;
            }
            return false;
        }

        public bool Contains(Unit unit)
        {
            if (!string.IsNullOrEmpty(SiteId) && !string.IsNullOrEmpty(unit.SiteId) && !string.IsNullOrEmpty(unit.UnitId) && SiteId.Equals(unit.SiteId))
            {
                Units.Any(u => u.SiteId.Equals(unit.SiteId) && u.UnitId.Equals(unit.UnitId));
            }
            return false;
        }

    }
}